<?php
require_once 'loader.php';
require_once 'Mobitv/Cronjobs/Alert.php';

$db = Vega_Dao::factory('Mobitv_Common')->mydb;
$db->query("select msisdn, content from transaction where action='addAddonChannel' and charge>0 and created_time>='2010-01-01' and created_time<'2010-02-01' group by msisdn, action, content having count(*)>1");
$rows = $db->fetchRowset();
if($rows)
{
    foreach($rows AS $row)
    {	$i++;
        $msisdn = $row['msisdn'];
        $content = $row['content'];
        $db->query("SELECT * FROM transaction WHERE msisdn='$msisdn' AND action='addAddonChannel' AND content='$content' AND created_time>'2010-01-01' ORDER BY created_time LIMIT 1");
        $tran = $db->fetchRow();
        if($tran)
        {
            $i++;
            $firstTime = $tran['created_time'];
            $sql = "DELETE FROM transaction WHERE msisdn='$msisdn' AND action='addAddonChannel' AND content='$content' AND created_time>'$firstTime' AND created_time<'2010-02-01'";

          $db->query($sql);
        }   echo "$i - $msisdn - $content - $firstTime\n";
    }
}
